<?php
class lib_site_admin extends lib_base{
	public $table_name = "qc_site_admin";
    public $pk = "id";

    public function check_user($username){
        $cond = "username='{$username}'";
        return $this->find(array($cond));
    }

    public function add_user($data)
    {
        $arrAdd = array(
            'username' => $data['username'],
            'password' => qcing_hash($data['password']),
            'add_time' => date('Y-m-d H:i:s'),
            'status'=>1,
        );
        $arrBase = array_merge($arrAdd,$data);
        return $this->create($arrBase);
    }

    public function edit_user($id,$data)
    {
        if($data['password']){
            $data['password'] = qcing_hash($data['password']);
        }
        return $this->edit($id,$data);
    }

    public function del_user($id=0)
    {
        if($id){
            return $this->delete(array("site_id={$id}"));
        }
        return true;
    }

    public function pwd_user($data){
        $username = $data['username'];
        $cond = "username='{$username}' and status=1";
        $admin = $this->find(array($cond));
        if($admin){
            $arrBase = array(
                'password'=>qcing_hash($data['password']),
            );
            $this->edit($admin['id'], $arrBase);
            return true;
        }
        return false;
     }

    
     public function get_user($user_id)
     {
         $cond = "status=1 and user_id='{$user_id}'";
         // $cond .= " and find_in_set('{$city_id}',area_ids)";
         // $cond .= " and find_in_set('{$cate_id}',cate_ids)";
         $admin = $this->get_info($cond);
         if($admin){
             return $admin['id'];
         }
         return false;
     }

    // public function get_user($user_id,$city_id,$cate_id)
    // {
    //     $cond = "status=1 and user_id='{$user_id}'";
    //     $cond .= " and find_in_set('{$city_id}',area_ids)";
    //     $cond .= " and find_in_set('{$cate_id}',cate_ids)";
    //     $user = $this->get_info($cond);
    //     if($user){
    //         return $user['id'];
    //     }
    //     return false;
    // }

    public function check_login($username,$password){
        $password = qcing_hash($password);
        $cond = "(username='{$username}' or tel='{$username}') and password='{$password}' and status=1";
        $admin = $this->find(array($cond));
        if($admin){
            // 生成token (session3rd)
            // 记录缓存, 7天
            $token = pp_encrypt($admin['id']);
            // Cache::set($token, $user['id'], 86400 * 7);

            $data['admin'] = $admin;
            $data['token'] = $token;
            return $data;
        }else{
            return false;
        }
    }

    public function up_time($id){
       $arrBase = array(
            'up_time'=>date('Y-m-d H:i:s'),
            'last_ip'=>get_ip(),
        );
        return $this->edit($id,$arrBase);
    }


    public function get_auth_list($user){
        if($user['area_ids']){
            $cond = "id IN ({$user['area_ids']}) and status=1";
            $all = D('site_area')->get_list($cond);
            $data['areas'] = list_to_tree($all);
        }
        if($user['cate_ids']){
            $cond = "id IN ({$user['cate_ids']}) and status=1";
            $all = D('site_cate')->get_list($cond);
            $data['cates'] = list_to_tree($all);
        }
        if($user['zone_ids']){
            $cond = "id IN ({$user['zone_ids']}) and status=1";
            $all = D('site_zone')->get_list($cond);
            $data['zones'] = list_to_tree($all);
        }
        return $data;
    }

    public function get_shifu_data($cond){
        
        $result = $this->findAll(array($cond),"{$this->pk} asc");
        // dump($result);exit;
        if($result){
            $tree = PHPTree::getTree($result,true);
            return $tree;
        }
        return false;
    }


    public function get_shifu_id($data,$config){
        
        //自动接单
        if($config['bx_moshi']==1){
            return 0;
        }

        $area_id = $data['area_id'];
        $cate_id = $data['cate_id'];
        $zone_id = $data['zone_id'];
        //全部师傅
        $cond = "status=1 and (level=3 or level=4) ";
        $cond1 = $cond;
        if($area_id){
            $cond .= " and find_in_set('{$area_id}',area_ids)";
            $cond1 .= " and area_id={$area_id}";
        }
        if($cate_id){
            $cond .= " and find_in_set('{$cate_id}',cate_ids)";
            $cond1 .= " and cate_id={$cate_id}";
        }
        if($zone_id){
            $cond .= " and find_in_set('{$zone_id}',zone_ids)";
            $cond1 .= " and zone_id={$zone_id}";
        }
        $shifus = $this->findAll(array($cond),'id asc');
        
        if($shifus){
            // dump($config);exit;
           
            //自动派单
            if($config['bx_moshi']==2){
                $shifu_id = $shifus[0]['id'];
                return $shifu_id;
            }
            //循环派单
            if($config['bx_moshi']==3){
                
                foreach ($shifus as $val) {
                    $shifu_arr[]=$val['id'];
                }
                $shifu_ids = implode(',', $shifu_arr);

                $max_shifu_id = max($shifu_arr);
                $min_shifu_id = min($shifu_arr);

                //上一次师傅
                $last_shifu_id = 0;
                $last_order = D('site_order')->find(array($cond1),'id desc');

                if($last_order){
                    $last_shifu_id = $last_order['shifu_id'];
                    $last_key = array_search($last_shifu_id,$shifu_arr);
                    $shifu_count = count($shifu_arr);

                    if(!$last_key || $last_key >= $shifu_count){
                         //第一位师傅
                        $shifu_id = $shifu_arr[0];
                    }else{
                        //下一位师傅
                        $shifu_id = $shifu_arr[$last_key+1];
                    }

                }else{
                    $shifu_id = $shifu_arr[0];
                }
                
                return $shifu_id;
            }


        }else{
            return 0;
        }
        
    }

}